<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>注册</title>
    <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.1/jquery.min.js"></script>
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css"
          integrity="sha384-HSMxcRTRxnN+Bdg0JdbxYKrThecOKuH5zCYotlSAcp1+c8xmyTe9GYg1l9a69psu" crossorigin="anonymous"/>
    <script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
</head>
<body>

<div th:insert="head.html"></div>


<div class="container-fluid" id="app" style="margin-top: 180px;margin-bottom: 100px">

    <div class="container">

        <div class="row">
            <a href="/login.html">已有账号?立即去登录</a>
            <div class="col-md-4 col-md-offset-4"
                 style="border: 1px solid #cccccc ;border-radius: 5px;margin-top: 10px">
                <form id="_form">
                    <p style="color: red" v-text="errorInfo">errorInfo</p>
                    <div class="form-group">
                        <label for="username">用户名:</label>
                        <input type="text" v-model="registerForm.username" name="username" class="form-control"
                               id="username" placeholder="请输入用户名">
                    </div>
                    <div class="form-group" >
                        <label for="password">密码</label>
                        <input type="password" v-model="registerForm.password" name="password" class="form-control"
                               id="password" placeholder="请输入密码">
                    </div>
                    <div class="form-group">
                        <label for="repass">确认密码:</label>
                        <input type="password" v-model="registerForm.repass" name="repass" class="form-control"
                               id="repass" placeholder="请输入确认密码">
                    </div>

                    <div class="form-group">
                        <label for="email">邮箱:</label>
                        <input type="email" v-model="registerForm.email" name="email" class="form-control" id="email"
                               placeholder="请输入邮箱地址">
                        <button class="btn btn-primary" @click.prevent="getEmailCode" v-text="authCode"
                                id="_authCodeBtn">获取验证码
                        </button>
                    </div>
                    <div class="form-group">
                        <label for="emailCode">请输入验证码:</label>
                        <input type="text" v-model="registerForm.emailCode" name="emailCode" class="form-control"
                               id="emailCode" placeholder="请输入邮箱验证码">
                    </div>
                    <button class="btn btn-success" @click.prevent="registerUser">注册</button>
                </form>
            </div>
        </div>

    </div>

</div>


<script>


    let app = new Vue({
        el: "#app",
        data: {
            registerForm: {
                username: '',
                password: '',
                repass: '',
                email: '',
                emailCode: ''
            },
            errorInfo: '',
            authCode: '获取验证码'
        },
        methods: {
            getEmailCode() {
                let email = this.registerForm.email
                let matchEmail = /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/
                if (!matchEmail.test(email)) {
                    this.errorInfo = '请输入正确格式的邮箱'
                    return;
                }
                this.countDown();
                $.post('/user/getEmailCode', {email: email},  data=> {
                    this.errorInfo = data
                    console.log(data);
                })
            },
            countDown() {
                let $codeBtn = $("#_authCodeBtn")
                $codeBtn.prop('disabled', true)
                let totalSeconds = 60;
                let interval = setInterval( ()=> {
                    this.authCode = totalSeconds + '秒后可重新获取验证码'
                    totalSeconds--
                    if (totalSeconds < 0) {
                        $codeBtn.prop('disabled', false)
                        this.authCode = '获取验证码'
                        clearInterval(interval)
                    }
                }, 1000);
            },
            registerUser() {
                let registerUser = this.registerForm
                if (!this.isFormSubmit()) {
                    this.errorInfo = '请完成下列表单'
                    return;
                }
                if (registerUser.password !== registerUser.repass) {
                    this.errorInfo = '两次密码应该一致'
                    return ;
                }

                let codeMatch = /^[0-9]{4}$/
                if (!codeMatch.test(registerUser.emailCode)) {
                    this.errorInfo = '请输入合适的验证码'
                    return ;
                }

                $.post('/user/registerUser', registerUser,  data=> {
                    this.errorInfo = ''
                    if (data !== "200"){
                        console.log(data)
                        this.errorInfo = data
                        return
                    }
                    location.href = '/'
                })
            },
            isFormSubmit() {
                let registerUser = this.registerForm;
                return registerUser.username !== '' && registerUser.password !== '' && registerUser.email !== '' && registerUser.emailCode !== ''
            }

        },
        created() {

        }
    })

</script>


</body>
</html>